package service.ucenter.controller;


import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.edu.result.R;
import service.edu.utils.JwtUtil;
import service.ucenter.entity.Member;
import service.ucenter.entity.vo.LoginMember;
import service.ucenter.entity.vo.RegisterMember;
import service.ucenter.service.MemberService;
import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author diao
 * @since 2020-09-10
 */
@RestController
@RequestMapping("/ucenter/")
@CrossOrigin
public class MemberController {
    @Autowired
    private MemberService memberService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public R  login(@RequestBody LoginMember loginMember){
        String token = memberService.login(loginMember);
        return R.Ok().data("token",token);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterMember registerMember){
        memberService.register(registerMember);
        return R.Ok();
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("/getMemberInfoByToken")
    public R getMemberInfoByToken(HttpServletRequest request){
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);
        if (member!=null){
            return R.Ok().data("member",member);
        }
        else {
            return R.Error();
        }
    }

    @ApiOperation("根据会员id获取会员信息,用于被微服务调用")
    @GetMapping("getMemberInfo/{memberId}")
    public Member getMemberInfoByMemberId(@PathVariable String memberId){
        return memberService.getById(memberId);
    }
}

